package com.aysafety.iotdataplatformserver.listener;

import com.alibaba.fastjson.JSON;
import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.CurrentBreakerModel;
import com.aysafety.iotdataplatformserver.dao.CurrentBreakerDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

// 电流开关量
@Component
public class CurrentBreakerListener {

    private final String type = "CurrentBreaker";

    private final String tableName = "CurrentBreakerRealTime";

    private final String[] structure = new String[] {"CurrentBreaker"};

    private Logger logger = LoggerFactory.getLogger(ErrorListener.class);

    @Autowired
    KsTreeDataListener ksTreeDataListener;

    @Autowired
    CurrentBreakerDao currentBreakerDao;

    public boolean createTable() throws IOException {
        return HBaseDriver.createTable(this.tableName, this.structure);
    }

    public void start(Map<String, Object> result) {
        try {
            String clientId = result.get("clientId").toString();
            if (!result.get("dataType").toString().equals("histroy")) {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    CurrentBreakerModel currentBreaker = this.transformData(listData);
                    this.save(currentBreaker, clientId);
                }
            } else {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    CurrentBreakerModel currentBreaker = this.transformData(listData);
                    this.save(currentBreaker, clientId);
                }
            }
        } catch (IOException e) {
            logger.info("CurrentBreakerListener:", e);
        }
    }

    public boolean save(CurrentBreakerModel currentBreaker, String clientId) throws IOException {
        String timestamp = String.valueOf(currentBreaker.getDatetime().getTime());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableId", currentBreaker.getEntId() + "." + "CurrentBreakerRealTime");
        map.put("pk", "\'" + timestamp  +".:." + currentBreaker.getDeviceId()+ "\'");
        map.put("clientId", "\'" + clientId + "\'");
        map.put("deviceId", "\'" + currentBreaker.getDeviceId() + "\'");
        map.put("entId", "\'" + currentBreaker.getEntId() + "\'");
        map.put("unit", "\'" + currentBreaker.getUnit() + "\'");
        map.put("workingFace", "\'" + currentBreaker.getWorkingFace() + "\'");
        map.put("datetime", currentBreaker.getDatetime().getTime());
        map.put("monitoringValue", currentBreaker.getMonitoringValue());
        map.put("poklad", currentBreaker.getPoklad());
        map.put("smallestRange", currentBreaker.getSmallestRange());
        map.put("maximumRange", currentBreaker.getMaximumRange());
        map.put("stationName", "\'" + currentBreaker.getStationName() + "\'");
        map.put("status", currentBreaker.isStatus());
        map.put("powerCutArea", "\'" + currentBreaker.getPowerCutArea() + "\'");
        map.put("powerReconnectionLimit", currentBreaker.getPowerReconnectionLimit());
        map.put("powerCutLimit", currentBreaker.getPowerCutLimit());
        currentBreakerDao.insertCurrentBreakerRealTime(map);
        return true;
    }

    public CurrentBreakerModel transformData(Map<String, Object> result) {
        CurrentBreakerModel currentBreaker = new CurrentBreakerModel();
        if (null != result.get("entId")) {
            currentBreaker.setEntId(result.get("entId").toString());
        } else {
            currentBreaker.setEntId("无数据");
        }
        if (null != result.get("deviceId")) {
            currentBreaker.setDeviceId(result.get("deviceId").toString());
        } else {
            currentBreaker.setDeviceId("无数据");
        }
        currentBreaker.setMonitoringValue(Double.valueOf(result.get("monitoringValue").toString()));
        if (null != result.get("poklad")) {
            currentBreaker.setPoklad(Double.valueOf(result.get("poklad").toString()));
        } else {
            currentBreaker.setPoklad(-1.0);
        }
        currentBreaker.setDatetime(DateUtils.fomatDateTime(Long.valueOf(result.get("datetime").toString())));
        if (null != result.get("workingFace")) {
            currentBreaker.setWorkingFace(result.get("workingFace").toString());
        } else {
            currentBreaker.setWorkingFace("无数据");
        }
        if (null != result.get("unit")) {
            currentBreaker.setUnit(result.get("unit").toString());
        } else {
            currentBreaker.setUnit("无数据");
        }
        if (null != result.get("smallestRange")) {
            currentBreaker.setSmallestRange(Double.valueOf(result.get("smallestRange").toString()));
        } else {
            currentBreaker.setSmallestRange(-1.0);
        }
        if (null != result.get("maximumRange")) {
            currentBreaker.setMaximumRange(Double.valueOf(result.get("maximumRange").toString()));
        } else {
            currentBreaker.setSmallestRange(-1.0);
        }
        if (null != result.get("stationName")) {
            currentBreaker.setStationName(result.get("stationName").toString());
        } else {
            currentBreaker.setUnit("无数据");
        }
        if (null != result.get("status")) {
            if (result.get("status").toString().equals("1")) {
                currentBreaker.setStatus(TRUE);
            } else {
                currentBreaker.setStatus(FALSE);
            }
        } else {
            currentBreaker.setStatus(FALSE);
        }
        if (null != result.get("powerCutArea")) {
            currentBreaker.setPowerCutArea(result.get("powerCutArea").toString());
        } else {
            currentBreaker.setPowerCutArea("无数据");
        }
        if (null != result.get("powerReconnectionLimit")) {
            currentBreaker.setPowerReconnectionLimit(Double.valueOf(result.get("powerReconnectionLimit").toString()));
        } else {
            currentBreaker.setPowerReconnectionLimit(-1.0);
        }
        if (null != result.get("powerCutLimit")) {
            currentBreaker.setPowerCutLimit(Double.valueOf(result.get("powerCutLimit").toString()));
        } else {
            currentBreaker.setPowerCutLimit(-1.0);
        }
        return currentBreaker;
    }
}
